/*
 * @Author: 杨上漂
 * @Date: 2024-10-13 10:27:42
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 18:32:39
 * @FilePath: \gwlc\assets\scripts\SpinArrow.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, ITriggerEvent, log, Node, RigidBody2D, v2, v3 } from 'cc';
import { randomNum } from '../utils/utils';
import { Player } from '../mainScene/Player';
import { playerSkillTypeEnum } from '../Enum';
import { Enemy } from '../mainScene/Enemy';
import { PoolMgr } from '../PoolMgr';

const { ccclass, property } = _decorator;

@ccclass('SpinArrow')
export class SpinArrow extends Component {

    attackTargetPos=null;

    tempPos=v3();

    init() {
        this.poolReset();
        this.node.getComponent(RigidBody2D).applyAngularImpulse(30,true);
        this.node.getComponent(RigidBody2D).applyForceToCenter(v2(randomNum(-1200,1200),randomNum(Player.ins.spinArrowForce-700,Player.ins.spinArrowForce+700)),true)


        let collider=this.node.getComponent(BoxCollider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerEnterFuc, this);
    }
    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
    }

    protected update(dt: number): void {
        if (Math.abs(this.node.position.x)>600||Math.abs(this.node.position.y)>1000) {//超出界面销毁
            this.recycle();
        }
    }

    onTriggerEnterFuc(self: Collider, other: Collider) {
            if (other.node.getComponent(BoxCollider2D).tag==1) {
                director.once(Director.EVENT_AFTER_PHYSICS,()=>{
                    other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.spinArrow);
                    // this.recycle();
                },this)
            }
    }

    recycle(){
        this.node.destroy();
        // PoolMgr.ins.poolPut(this.node.name,this.node);
    }
 
 
    
 
}


